package cn.sciento.transfer.infra.datasource.ddl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import cn.sciento.core.util.FieldNameUtils;
import cn.sciento.transfer.infra.datasource.ddl.utils.ResultSetUtils;
import cn.sciento.transfer.infra.datasource.sql.SqlUtils;
import org.springframework.dao.DataAccessException;

public abstract class BaseTableScriptBuilder implements TableScriptBuilder {
  public List<Map<String, Object>> doQuery(Connection connection, String sql, Boolean convertCamel) throws SQLException {
    return (List<Map<String, Object>>)SqlUtils.executeWithInOnceStatement(connection, statement -> {
          ResultSet resultSet = statement.executeQuery(sql);
          return ResultSetUtils.changeResultSetToList(resultSet, convertCamel);
        });
  }
  
  public List<String> parsePrimaryKey(String indexColumnNames) {
    List<String> primaryKeys = new ArrayList<>();
    List<String> stringList = Arrays.asList(indexColumnNames.split(","));
    stringList.forEach(item -> primaryKeys.add(FieldNameUtils.underline2Camel(item, true)));
    return primaryKeys;
  }
}
